package com.example.demo.service.impl;

import com.example.demo.mapper.UserInfoMapper;
import com.example.demo.pojo.UserInfo;
import com.example.demo.service.UserInfoService;
import com.example.demo.utils.LoggerConfig;
import com.example.demo.utils.RedisUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(value = "UserInfoService")
@Transactional()
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    public UserInfoMapper userInfoDao;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public boolean addUserInfo(UserInfo userInfoModel) {
        if (queryPhone(userInfoModel.getMobile())) {
            LoggerConfig.getLoggerError("注册失败,该手机号已注册");
            return false;
        } else {
            return userInfoDao.insertUserInfo(userInfoModel)>0;
        }
    }

    @Override
    public List<UserInfo> login(String mobile, String passWord) {
        return userInfoDao.login(mobile, passWord);
    }

    @Override
    public boolean queryPhone(String mobile) {
        List<UserInfo> userList = userInfoDao.queryPhone(mobile);
        return userList.size() > 0;
    }

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> findAllVipInfo(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return userInfoDao.queryAllUserInfo();
    }
}
